#!/bin/bash

# Java环境诊断脚本
echo "🔍 Java环境诊断"
echo "=============="

# 1. 检查系统Java版本
echo "1. 系统Java版本:"
java -version 2>&1

echo ""

# 2. 检查JAVA_HOME
echo "2. JAVA_HOME环境变量:"
echo "JAVA_HOME: $JAVA_HOME"
if [ -n "$JAVA_HOME" ]; then
    if [ -d "$JAVA_HOME" ]; then
        echo "✅ JAVA_HOME目录存在"
    else
        echo "❌ JAVA_HOME目录不存在"
    fi
else
    echo "⚠️  JAVA_HOME未设置"
fi

echo ""

# 3. 检查PATH中的Java
echo "3. PATH中的Java路径:"
which java

echo ""

# 4. 检查已安装的Java版本
echo "4. 系统已安装的Java版本:"
if command -v /usr/libexec/java_home &> /dev/null; then
    /usr/libexec/java_home -V 2>&1
else
    echo "❌ java_home命令不可用"
fi

echo ""

# 5. 检查Maven配置
echo "5. Maven配置:"
if command -v mvn &> /dev/null; then
    mvn -version | grep "Java version"
else
    echo "❌ Maven未安装"
fi

echo ""

# 6. 检查项目编译
echo "6. 项目编译测试:"
echo "运行: mvn clean compile -DskipTests"
mvn clean compile -DskipTests 2>&1 | tail -10

echo ""

# 7. 提供解决方案
echo "🔧 解决方案:"
echo "如果还有问题，请运行:"
echo "  ./fix-java17.sh    # 自动修复Java 17配置"
echo "  source ~/.zshrc    # 重新加载配置"
echo "  java -version      # 验证版本"